<template>
  <div class="login" ref="DomLogin">
    <!-- logo -->
    <div class="logo">
      <span class="text">GMALL</span>
    </div>
    <!-- 登录框 -->
    <el-row class="login-container">
      <!-- slogan -->
      <el-col :span="12" class="slogan-wrapper">
        <p>Ai赋能智慧零售</p>
        <p>用户也许没那么复杂</p>
      </el-col>
      <!-- 登录模块 -->
      <el-col :span="12" class="login-wrapper">
        <div class="login-box-title">登录GMALL</div>
        <div class="login-box-form">
          <div class="input-wrapper email">
            <input type="text"
                   placeholder="请输入账号"
                   v-model="username"
                   @focus="focusLoginInput"
                   @blur="blurLoginInput('account')"
                   class="login-box-input email-input"
                   :class="{ 'error-border': isEmptyAccount }">
          </div>
          <div class="input-wrapper password">
            <input type="password"
                   placeholder="请输入密码"
                   v-model="password"
                   @focus="focusLoginInput"
                   @blur="blurLoginInput('password')"
                   class="login-box-input password-input"
                   :class="{ 'error-border': isEmptyPassword }">
          </div>
          <div class="help-tips tips" v-if="!loginErrText">忘记密码请联系系统管理员</div>
          <div class="error-tips tips" v-else>
            {{ loginErrText }}
          </div>
        </div>
        <!-- 登录 button -->
        <div class="login-box-btn" @click="commitlogin">
          登录
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { Row, Col } from 'element-ui'
import { APIGmallLogin } from '@/api/system.js'
import Store2 from 'store2'
import Config from '@/utils/config.js'

export default {
  name: 'login',

  components: {
    // elementUI component
    elRow: Row,
    elCol: Col
  },
  // 添加监听回车事件
  created () {
    window.addEventListener('keyup', this.keyupEntryEvent)
  },
  // 移除事件
  beforeDestroy () {
    window.removeEventListener('keyup', this.keyupEntryEvent)
  },

  data () {
    return {
      username: '', // 登录账号
      password: '', // 登录密码
      loginErrText: '', // 错误文本内容
      isEmptyAccount: false, // 账户是否填写
      isEmptyPassword: false // 密码是否填写
    }
  },

  methods: {
    // 如果用户点击回车, 触发表单提交
    keyupEntryEvent (e) {
      if (e.key === 'Enter') {
        this.commitlogin()
      }
    },
    // 提交登录表单
    commitlogin () {
      if (!this.username && !this.password) {
        this.loginErrText = '请输入账号与密码'
        this.isEmptyAccount = true
        this.isEmptyPassword = true
      } else if (!this.username) {
        this.loginErrText = '请输入账号'
        this.isEmptyAccount = true
      } else if (!this.password) {
        this.loginErrText = '请输入密码'
        this.isEmptyPassword = true
      } else {
        this.login({ username: this.username, password: this.password })
      }
    },
    // 调用登录API
    login ({username, password}) {
      APIGmallLogin({ username, password })
        .then((res) => {
          this.$store.dispatch('saveToken', res.data.token)
          Store2(Config.TOKEN_STORAGE_KEY, res.data.token)
          this.$router.replace({ name: 'compass' })
        })
    },
    // 登录输入框聚焦触发的事件
    focusLoginInput () {
      this.loginErrText = ''
    },
    // 登录输入框失焦触发的事件
    blurLoginInput (domname) {
      switch (domname) {
        case 'account':
          // 如果输入框不是红框状态时, 空输入不标红色
          if (this.isEmptyAccount) {
            this.isEmptyAccount = Boolean(!this.username)
          }
          break
        case 'password':
          if (this.isEmptyPassword) {
            this.isEmptyPassword = Boolean(!this.password)
          }
          break
      }
    }
  }
}
</script>

<style scoped lang="less" rel="stylesheet/less">
  .login {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    background-image: url('./images/login-bg.png');
    .logo {
      position: absolute;
      left: 40px;
      top: 20px;
      color: #EEE;
      .text {
        height: 20px;
        line-height: 20px;
        font-size: 20px;
      }
    }
    .login-container {
      box-sizing: border-box;
      position: absolute;
      top: 50%;
      left: 50%;
      width: 680px;
      padding: 40px 0;
      transform: translate(-50%, -50%);
      border-radius: 4px;
      text-align: center;
      background-color: #ffffff;
      .slogan-wrapper {
        padding: 90px 0;
        font-size: 24px;
        line-height: 30px;
        letter-spacing: 1px;
      }
      .login-wrapper {
        padding: 0 50px;
        border-left: 1px dashed #cdcdcd;
        .login-box-title {
          margin: 0 0;
          padding: 40px 0;
          font-size: 24px;
          color: #444;
          text-align: center;
          font-weight: normal;
        }
        .login-box-form {
          .input-wrapper {
            font-size: 14px;
            &:first-child {
              margin-bottom: 15px;
            }
            .login-box-input {
              box-sizing: border-box;
              width: 100%;
              height: 38px;
              background-color: #FFF;
              border: 1px solid #E2E2E2;
              border-radius: 2px;
              padding-left: 10px;
              outline: 0;
              &:focus, &:hover {
                border-color: #409EF7 !important;
              }
              &.error-border {
                border-color: #e54545;
              }
            }
          }
          .tips {
            margin-top: 5px;
            margin-left: 10px;
            font-size: 12px;
            &.help-tips {
              color: #666;
              text-align: right;
            }
            &.error-tips {
              text-align: left;
              color: #e54545;
            }
          }
        }
        .login-box-btn {
          margin: 25px 0 40px;
          width: 100%;
          height: 40px;
          line-height: 40px;
          background-color: #2D384D;
          color: #FFF;
          border-radius: 2px;
          box-sizing: border-box;
          font-size: 16px;
          letter-spacing: 5px;
          outline: 0;
          cursor: pointer;
        }
      }
    }
  }
</style>
